1553B - Reverse String - CodeForces Solution


brute force dp hashing implementation strings *1300

Please click on ads to support us..

Python Code:

t = int(input())
for i in range(t):
    s = input()
    s2 = input()
    for j in range(len(s)):
        if s2 in (s[:j] + s[j::-1]):
            print("YES")
            break
    else:
        print("NO")

C++ Code:

#include <bits/stdc++.h>

using namespace std;


/*
 	dacaba
	cabac

	p = 2



*/

int main() {

	int tt;
	cin >> tt;

	while (tt--) {

		string s, t;
		cin >> s >> t;

		vector<int> pos;
		const int n = s.size();
		const int m = t.size();

		for (int i = 0; i != n; ++i)
			if (s[i] == t[0])
				pos.push_back(i);


		bool found = false;

		for (auto& p : pos) {
			// Moving to the right
			for (int i = 1; i <= m && p + (i-1) < n; ++i) {
				string right = s.substr(p, i);
				int rem = m - right.size();
				
				if (p+i-rem-1 >= 0) {
				string left = s.substr(p+i-rem-1, rem);
				reverse(left.begin(), left.end());
				if (right + left == t) {
					found = true;
					cout << "YES\n";
					goto end;
				}
				}
			}
		}

end:
		if (!found) {
		cout << "NO\n";
		}
	}

	return 0;
}


Comments

Submit
0 Comments
More Questions

1717B - Madoka and Underground Competitions
61B - Hard Work
959B - Mahmoud and Ehab and the message
802G - Fake News (easy)
1717C - Madoka and Formal Statement
420A - Start Up
1031A - Golden Plate
1559C - Mocha and Hiking
427B - Prison Transfer
330A - Cakeminator
426A - Sereja and Mugs
363A - Soroban
1585C - Minimize Distance
1506E - Restoring the Permutation
1539A - Contest Start
363D - Renting Bikes
1198D - Rectangle Painting 1
1023B - Pair of Toys
1725A - Accumulation of Dominoes
1675E - Replace With the Previous Minimize
839A - Arya and Bran
16B - Burglar and Matches
1625B - Elementary Particles
1725G - Garage
1725B - Basketball Together
735A - Ostap and Grasshopper
1183B - Equalize Prices
1481A - Space Navigation
1437B - Reverse Binary Strings
1362B - Johnny and His Hobbies